//
//  CHSViewController.m
//  CAIPluginsManager
//
//  Created by 海参 on 01/05/2022.
//  Copyright (c) 2022 海参. All rights reserved.
//

#import "CHSViewController.h"

#import "CHSTableViewCell.h"
#import <CAIPluginsManager/CAIPluginsManager.h>

@interface CHSViewController ()<UITableViewDataSource,UITableViewDelegate>



@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *dataMArray;

@property (strong, nonatomic) CAIPluginsManager *manager;

@end

@implementation CHSViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self setUI];
    
    [self setData];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self requestData];

}


- (void)setUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
//    [self.menuIconCollectionView zx_setBorderWithCornerRadius:LCDScale_iPhone6(8) borderWidth:0.5 borderColor:nil];
}

- (void)setData{
    
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:8];
    for (int i=0; i<8; i++) {
        
        MenuItemModel *model = [[MenuItemModel alloc] init];
//        model.icon = @"https://pics1.baidu.com/feed/d53f8794a4c27d1e6e6048090c831467dcc4381c.jpeg?token=c574cfbb1da47c01d0f939e734e15e49";
        model.title = @"标题";
        model.sideMarkType = SideMarkTypeNone;
        [mArray addObject:model];
    }
    self.dataMArray = mArray;
}


- (CAIPluginsManager *)manager{
    if (!_manager) {
        _manager = [CAIPluginsManager new];
        _manager.pluginTimeInterval = 10;
        _manager.isStartPolling = YES;
    }
    return _manager;
}

- (void)requestData{
    [self.manager getPluginsWithModule:nil success:^(NSArray<PluginsModel *> * _Nonnull plugins) {
           
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:8];
        [plugins enumerateObjectsUsingBlock:^(PluginsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MenuItemModel *model = [[MenuItemModel alloc] init];
            model.icon = obj.icon;
            model.title = obj.title;
            model.sideMarkType = SideMarkTypeNone;
            model.route = obj.route;
            [mArray addObject:model];
        }];
        self.dataMArray = mArray;
        [self.tableView reloadData];
        
        } failureBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
            
    }];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        UITableView *view = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        view.delegate = self;
        view.dataSource = self;
//        view.backgroundColor = [UIColor caiColorWithHexString:@"#F5F5F5"];
        view.separatorStyle = UITableViewCellSeparatorStyleNone;
        view.rowHeight = UITableViewAutomaticDimension;
        [view registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        [view registerClass:[CHSTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CHSTableViewCell class])];
        _tableView = view;
    }
    return _tableView;
}

#pragma mark - DataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
        cell.textLabel.text = @"Cell1";
        return  cell;
    }
    CHSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CHSTableViewCell class]) forIndexPath:indexPath];
    [cell setData:self.dataMArray];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section ==1)
    {
        static CHSTableViewCell *cell =  nil;
        static dispatch_once_t onceToken;
        
        dispatch_once(&onceToken, ^{
            
            cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CHSTableViewCell class])];
        });
        return [cell getCellHeightWithContentIndexPath:indexPath data:self.dataMArray];
    }
    return 45;
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
